package com.wwwarehouse.taskcenter.fragment.invite_business_information;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.adapter.InviteSuccessAdapter;
import com.wwwarehouse.taskcenter.bean.InviteRankFilterBean;
import com.wwwarehouse.taskcenter.bean.InviteSuccessBean;
import com.wwwarehouse.taskcenter.constant.TaskCenterConstant;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @创建者 chenchao.
 * @创建时间 2017/10/11 11:03
 * @描述 ${TODO}
 */

public class InviteSuccessFragmentChild extends BaseFragment implements View.OnClickListener {

    private View           mView;
    private StateLayout    mStateLayout;
    private ListView       mListView;
    private TextView       mTvName;
    private RelativeLayout mRlDetail;
    private Map            mInviteSuccessChildMap;
    private int            mPage;
    private int            mInviterId;
    private String         mInviterName;
    private boolean                    isDay;
    private boolean                    isWeek;
    private boolean                    isMonth;
    private boolean                    isYear;
    private InviteRankFilterBean       mMInviteRankFilterBean1;
    private InviteRankFilterBean       mMInviteRankFilterBean2;
    private InviteRankFilterBean       mMInviteRankFilterBean3;
    private InviteRankFilterBean       mMInviteRankFilterBean4;
    private InviteRankFilterBean       mMInviteRankFilterBean5;
    private InviteRankFilterBean       mMInviteRankFilterBean6;
    private String mStartTime;
    private String mEndTime;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_invite_success_child, container, false);
        return mView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();
    }

    private void initView() {

        mStateLayout = findView(mView, R.id.sl_state);
        mListView = findView(mView, R.id.lv_own_busi_unit);
        mTvName = findView(mView, R.id.tv_name);
        mRlDetail = findView(mView, R.id.rl_detail);
        mRlDetail.setOnClickListener(this);
    }

    private void initData() {
        Bundle inviteSuccessChildBundle = getArguments();
        if (inviteSuccessChildBundle != null) {
            mPage = inviteSuccessChildBundle.getInt("page");
            mInviterId = inviteSuccessChildBundle.getInt("inviterId");
            mInviterName = inviteSuccessChildBundle.getString("mInviterName");
            mStartTime = inviteSuccessChildBundle.getString("startTime");
            mEndTime = inviteSuccessChildBundle.getString("endTime");
            mTvName.setText(mInviterName);


            //day
            if (inviteSuccessChildBundle.getSerializable("mInviteRankFilterBean1") != null && inviteSuccessChildBundle.getSerializable("mInviteRankFilterBean2") != null) {
                isDay = true;
                mMInviteRankFilterBean1 = (InviteRankFilterBean) inviteSuccessChildBundle.getSerializable("mInviteRankFilterBean1");
                mMInviteRankFilterBean2 = (InviteRankFilterBean) inviteSuccessChildBundle.getSerializable("mInviteRankFilterBean2");
                //week
            } else if (inviteSuccessChildBundle.getSerializable("mInviteRankFilterBean1") != null && inviteSuccessChildBundle.getSerializable("mInviteRankFilterBean3") != null && inviteSuccessChildBundle.getSerializable("mInviteRankFilterBean4") != null) {
                isWeek = true;
                mMInviteRankFilterBean1 = (InviteRankFilterBean) inviteSuccessChildBundle.getSerializable("mInviteRankFilterBean1");
                mMInviteRankFilterBean3 = (InviteRankFilterBean) inviteSuccessChildBundle.getSerializable("mInviteRankFilterBean3");
                mMInviteRankFilterBean4 = (InviteRankFilterBean) inviteSuccessChildBundle.getSerializable("mInviteRankFilterBean4");
                //month
            } else if (inviteSuccessChildBundle.getSerializable("mInviteRankFilterBean1") != null && inviteSuccessChildBundle.getSerializable("mInviteRankFilterBean5") != null) {
                isMonth = true;
                mMInviteRankFilterBean1 = (InviteRankFilterBean) inviteSuccessChildBundle.getSerializable("mInviteRankFilterBean1");
                mMInviteRankFilterBean5 = (InviteRankFilterBean) inviteSuccessChildBundle.getSerializable("mInviteRankFilterBean5");
                //year
            } else if (inviteSuccessChildBundle.getSerializable("mInviteRankFilterBean1") != null && inviteSuccessChildBundle.getSerializable("mInviteRankFilterBean6") != null) {
                isYear = true;
                mMInviteRankFilterBean1 = (InviteRankFilterBean) inviteSuccessChildBundle.getSerializable("mInviteRankFilterBean1");
                mMInviteRankFilterBean6 = (InviteRankFilterBean) inviteSuccessChildBundle.getSerializable("mInviteRankFilterBean6");
            }
        }
        mInviteSuccessChildMap = new HashMap();
        mInviteSuccessChildMap.put("inviteeStatus", 10);
        mInviteSuccessChildMap.put("inviterId", mInviterId);
        mInviteSuccessChildMap.put("page", mPage);
        mInviteSuccessChildMap.put("size", 8);
        mInviteSuccessChildMap.put("sort", "update_time desc");
        mInviteSuccessChildMap.put("startTime", mStartTime);
        mInviteSuccessChildMap.put("endTime", mEndTime);
        if (inviteSuccessChildBundle != null) {
            getInviteSuccessChild();
        }
    }


    private void getInviteSuccessChild() {
        mStateLayout.showProgressView(true);//加载时候的状态
        NoHttpUtils.httpPost(TaskCenterConstant.GETINVITEINFO, mInviteSuccessChildMap, mOnResponseListener, 0);
    }

    private NoHttpUtils.OnResponseListener mOnResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int i) {
            try {
                switch (i) {
                    case 0:
                        if ("0".equals(commonClass.getCode())) {
                            InviteSuccessBean inviteSuccessBean = JSON.parseObject(commonClass.getData().toString(), InviteSuccessBean.class);
                            final List<InviteSuccessBean.ListBean> inviteSuccessChildList = inviteSuccessBean.getList();
                            InviteSuccessAdapter inviteSuccessAdapter = new InviteSuccessAdapter(mActivity, inviteSuccessChildList, mListView);
                            mListView.setAdapter(inviteSuccessAdapter);

                            mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                    Bundle bundle = new Bundle();
                                    bundle.putInt("inviteeId", inviteSuccessChildList.get(position).getInviteeId());
                                    pushFragment(TaskCenterConstant.OwnBusinessUnitFragment,bundle);
                                }
                            });
                        } else {
                            mStateLayout.showSystemView(commonClass.getMsg(), true);
                            mStateLayout.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    getInviteSuccessChild();
                                }
                            });
                        }

                        break;
                }
            } catch (Exception e) {
                LogUtils.showLog(e.toString());
            }
        }

        @Override
        public void onFailed(String s, int i) {
            mStateLayout.showNetworkView(true);
            mStateLayout.setNetworkListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getInviteSuccessChild();
                }
            });
        }

        @Override
        public void onFinish(int i) {
            mStateLayout.showContentView();
        }
    };

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.rl_detail) {
            Bundle toInviteCountBundle = new Bundle();
            if (isDay) {
                toInviteCountBundle.putSerializable("mInviteRankFilterBean1", mMInviteRankFilterBean1);
                toInviteCountBundle.putSerializable("mInviteRankFilterBean2", mMInviteRankFilterBean2);
                toInviteCountBundle.putInt("inviterId", mInviterId);
            } else if (isWeek) {
                toInviteCountBundle.putSerializable("mInviteRankFilterBean1", mMInviteRankFilterBean1);
                toInviteCountBundle.putSerializable("mInviteRankFilterBean3", mMInviteRankFilterBean3);
                toInviteCountBundle.putSerializable("mInviteRankFilterBean4", mMInviteRankFilterBean4);
                toInviteCountBundle.putInt("inviterId", mInviterId);
            } else if (isMonth) {
                toInviteCountBundle.putSerializable("mInviteRankFilterBean1", mMInviteRankFilterBean1);
                toInviteCountBundle.putSerializable("mInviteRankFilterBean5", mMInviteRankFilterBean5);
                toInviteCountBundle.putInt("inviterId", mInviterId);
            } else if (isYear) {
                toInviteCountBundle.putSerializable("mInviteRankFilterBean1", mMInviteRankFilterBean1);
                toInviteCountBundle.putSerializable("mInviteRankFilterBean6", mMInviteRankFilterBean6);
                toInviteCountBundle.putInt("inviterId", mInviterId);
            }
            pushFragment(TaskCenterConstant.InviteCountFragment, toInviteCountBundle);
        }

    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof InviteSuccessFragmentChild) {
            mActivity.setTitle("邀请成功名单");
        }
    }

    @Override
    public void requestDatas() {

    }
}
